//
//  DistributionMapCell.m
//  MonitoringNetwork
//
//  Created by admin on 2018/1/4.
//  Copyright © 2018年 wtmcxlm. All rights reserved.
//

#import "DistributionMapCell.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import "UIImage+Color.h"

@interface DistributionMapCell()<MAMapViewDelegate,MAMultiPointOverlayRendererDelegate>
@property (nonatomic,strong)MAMapView *mapView;
@property (nonatomic,strong)UILabel *titleLabel;
@end

@implementation DistributionMapCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI
{
    
    UIView *backView = [UIView new];
    backView.backgroundColor = [UIColor colorWithRed:0.97f green:0.97f blue:0.97f alpha:1.00f];
    [self addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self);
        make.height.mas_equalTo(11);
    }];
    
    _titleLabel = [UILabel LabelWithFont:TitleLagerFontSize andColor:TitleColor andTextAlignment:left andString:@"车辆分布图"];
    [self addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self).offset(33);
        make.centerY.mas_equalTo(self.mas_top).offset(11+29*0.5);
    }];
    
    _mapView = [[MAMapView alloc]initWithFrame:CGRectMake(0, 40,kScreenWidth, self.bounds.size.height-40)];
    _mapView.showsScale = NO;
    _mapView.showsCompass = NO;
    _mapView.zoomLevel = 3;
    _mapView.rotateEnabled = NO;
    _mapView.rotateCameraEnabled = NO;
    _mapView.delegate = self;
    MACoordinateSpan span = MACoordinateSpanMake(50, 62);
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(28, 104);
    MACoordinateRegion regoin = MACoordinateRegionMake(coordinate, span);
    _mapView.region = regoin;
    [self addSubview:_mapView];
}

- (void)loadDistributionMapWithArr:(NSArray *)arr
{
    if (arr.count == 0) {
        return;
    }
    
    ///创建MultiPointItems数组，并更新数据
    NSMutableArray *items = [NSMutableArray array];
    @autoreleasepool {
        
        if (arr)
        {
            for (NSDictionary *tempDict in arr){
                NSArray *dicArray = tempDict[@"point"];
                for (NSArray *point in dicArray)
                {
                    MAMultiPointItem *item = [[MAMultiPointItem alloc] init];
                    CLLocationCoordinate2D coordinate;
                    coordinate.latitude = [[point lastObject] doubleValue];
                    coordinate.longitude = [[point firstObject] doubleValue];
                    item.coordinate = coordinate;
                    [items addObject:item];
                }
            }
        }
    }
    
    if (items.count == 0) {
        return;
    }
    ///根据items创建海量点Overlay MultiPointOverlay
    MAMultiPointOverlay *_overlay = [[MAMultiPointOverlay alloc] initWithMultiPointItems:items];
    ///把Overlay添加进mapView
    [self.mapView addOverlay:_overlay];
}


- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
    if ([overlay isKindOfClass:[MAMultiPointOverlay class]])
    {
        MAMultiPointOverlayRenderer * renderer = [[MAMultiPointOverlayRenderer alloc] initWithMultiPointOverlay:overlay];
        UIImage *image = [UIImage imageWithColor:[UIColor colorWithRed:0.10f green:0.47f blue:0.74f alpha:1.00f] size:CGSizeMake(10, 10)];
  
        ///设置图片
        renderer.icon = image;
        ///设置锚点
        renderer.anchor = CGPointMake(0.5, 1.0);
        renderer.delegate = self;
        return renderer;
    }
    
    return nil;
}
- (void)multiPointOverlayRenderer:(MAMultiPointOverlayRenderer *)renderer didItemTapped:(MAMultiPointItem *)item
{
    NSLog(@"item :%@ <%f, %f>", item, item.coordinate.latitude, item.coordinate.longitude);
}
@end
